package com.lin.lostandfound.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lin.lostandfound.mapper.NoticeMapper;
import com.lin.lostandfound.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.lin.lostandfound.bean.Notice;
import com.lin.lostandfound.service.INoticeService;

@Service("noticeService")
public class NoticeService implements INoticeService {
	
	@Autowired
	private NoticeMapper mapper;

	@Autowired
	private StringUtil util;

	@Override
	public boolean add(Notice notice) {
		
		return mapper.insert(notice)!=-1;
	}

	@Override
	public boolean modifyStatus(Long id, int status) {
		
		return false;
	}

	@Override
	public boolean remove(Integer id) {
		
		return mapper.deleteById(id)!=-1;
	}
	
	// 查询最新一条
	@Override
	public Notice queryNewNotice() {

		QueryWrapper<Notice> wrapper = new QueryWrapper<>();

		wrapper.orderByDesc("publishTime");

		return mapper.selectList(wrapper).get(0);
	}

	@Override
	public long queryAllCount(String key) {
		

		QueryWrapper<Notice> wrapper = new QueryWrapper<>();
		if(util.notnull(key)){
			wrapper.like("title",key);
		}
		int i = mapper.selectCount(wrapper);
		return i;

	}

	@Override
	public Map<String, Object> queryAll(String key, Long pageIndex, Long pageSize, String sortField, String sortOrder) {

		Page<Notice> page_ = new Page<>(pageIndex,pageSize);
		QueryWrapper<Notice> wrapper = new QueryWrapper<>();
		if(util.notnull(key)){
			wrapper.like("title",key);

		}
		if (util.notnull(sortField)) {
			if(sortOrder.equals("desc")){
				wrapper.orderByDesc(sortField);
			}else{
				wrapper.orderByAsc(sortField);
			}
		}else{
			wrapper.orderByDesc("publishTime");
		}
		IPage<Notice> page=mapper.selectPage(page_,wrapper);


		Map<String, Object> map = new HashMap<String, Object>();
		map.put("data", page.getRecords());
		map.put("total", page.getTotal());

		return map;
	}

	@Override
	public Notice queryById(int id) {

		return mapper.selectById(id);
	}

}
